/**
 * 
 */
package com.epam.training.service.bookingitem;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.epam.training.domain.BookingItem;
import com.epam.training.domain.Paymant;
import com.epam.training.repository.BookingItemRepository;
import com.epam.training.repository.PaymantRepository;

/**
 * @author Bogdan
 *
 */

@Service
public class BookingItemServiceImpl implements BookingItemService {
	private static final int PAGE_SIZE = 10;

	private final BookingItemRepository bookingItemRepository;
	private final PaymantRepository paymantRepository;

	@Autowired
	public BookingItemServiceImpl(BookingItemRepository bookingItemRepository,
			PaymantRepository paymantRepository) {
		this.bookingItemRepository = bookingItemRepository;
		this.paymantRepository = paymantRepository;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.epam.training.service.bookingitem.BookingItemService#getAllBookingItem
	 * (java.lang.Integer)
	 */
	@Override
	public Page<BookingItem> getAllBookingItem(Integer pageNumber) {
		PageRequest pageRequest = new PageRequest(pageNumber - 1, PAGE_SIZE);
		return bookingItemRepository.findAll(pageRequest);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.epam.training.service.bookingitem.BookingItemService#save(com.epam
	 * .training.domain.BookingItem)
	 */
	@Transactional
	@Override
	public void save(BookingItem bookingItem) {
		bookingItemRepository.save(bookingItem);
	}

	@Override
	public List<BookingItem> getAllBookingItemForUser(String login) {
		return bookingItemRepository.findByCustomerLogin(login);
	}

	@Override
	public BookingItem getById(Integer id) {
		return bookingItemRepository.findOne(id);
	}

	@Transactional
	@Override
	public void addPaymant(Integer id, Integer paymantId) {
		BookingItem booking = bookingItemRepository.findOne(id);
		Paymant paymant = paymantRepository.findOne(paymantId);

		booking.setPaymant(paymant);
		booking.setStatus("active");

		bookingItemRepository.save(booking);
	}

}
